package view.components
{
	import flash.events.MouseEvent;

	public class RadioGroup
	{
		private var currentSelectedRadio:Radio;
		private var radios:Array;
		
		public function RadioGroup()
		{
			init();
		}
	
		private function init():void
		{
			radios = [];
		}
		
		public function addRadio(radio:Radio):void
		{
			if (radios.indexOf(radio) < 0)
			{
				radios.push(radio);
				addRadioListener(radio);
			}
			
			if (radio.selected)
			{
				currentSelectedRadio = radio;
			}
		}
		
		public function get selectedIndex():int
		{
			return currentSelectedRadio.id;
		}
		
		public function set selectedIndex(v:int):void
		{
			for (var i:int=0; i<radios.length; i++)
			{
				if (i == v)
				{
					radios[i].selected = true;
					currentSelectedRadio = radios[i];
				}
				else
				{
					radios[i].selected = false;
				}
			}
		}
		
		private function addRadioListener(radio:Radio):void
		{
			radio.addEventListener(MouseEvent.CLICK,onRadioClick);
		}
		
		private function onRadioClick(event:MouseEvent):void
		{
			currentSelectedRadio = event.target as Radio;
			for each (var radio:Radio in radios)
			{
				if (radio != currentSelectedRadio)
				{
					radio.selected = false;
				}
			}
			currentSelectedRadio.selected = true;
		}
	}
}